package vip.geeker.iotms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.geeker.iotms.dto.DeviceTypeDto;
import vip.geeker.iotms.help.BusinessException;
import vip.geeker.iotms.help.BusinessResponseCode;
import vip.geeker.iotms.help.Response;
import vip.geeker.iotms.persistance.mapper.DeviceTypeMapper;
import vip.geeker.iotms.persistance.model.DeviceType;
import vip.geeker.iotms.service.IDeviceTypeService;
import vip.geeker.iotms.utils.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 设备类型服务
 *
 * @author zhoup
 * @create 2020-04-2711:23:00
 * @Email acacio@foxmail.com
 * To change this template use File | Settings | Editor | File and Code Templates.
 **/
@Service
@Transactional
public class DeviceTypeService extends ServiceImpl<DeviceTypeMapper, DeviceType> implements IDeviceTypeService {
    @Resource
    private DeviceTypeMapper deviceTypeMapper;

    public PageInfo<DeviceType> queryPage(DeviceType type) {
        PageHelper.startPage(type.getPageNo(), type.getPageSz());
        List<DeviceType> list = deviceTypeMapper.queryDeviceTypeList(type);
        return new PageInfo<>(list);
    }

    public List<DeviceType> queryDeviceTypeList(DeviceType type) {
        List<DeviceType> list = deviceTypeMapper.queryDeviceTypeList(type);
        return list;
    }

    @Transactional(readOnly = false)
    public Response addDeviceType(DeviceType type) {
        if (ObjectUtils.isNullOrEmpty(type.getName())) {
            throw new BusinessException(BusinessResponseCode.BAD_REQUEST, "类型名称不能为空");
        }
        List<DeviceType> deviceTypes = this.queryDeviceTypeList(type);
        if (ObjectUtils.isNotNullAndEmpty(deviceTypes)) {
            throw new BusinessException(BusinessResponseCode.BAD_REQUEST, "已经存在同名设备类型");
        }
        type.setCreateTime(new Date());
        type.setId(deviceTypeMapper.nextVal());
        deviceTypeMapper.insertSelective(type);
        return Response.successMsg("操作成功");
    }

    @Override
    public List<DeviceTypeDto> queryDeviceTypeTree(DeviceTypeDto deviceTypeDto) {
        return deviceTypeMapper.queryDeviceTypeTree(deviceTypeDto);
    }

    @Override
    public DeviceType selectByPrimaryKey(Integer id) {
        return deviceTypeMapper.selectByPrimaryKey(id);
    }

    @Transactional(readOnly = false)
    @Override
    public int updateByPrimaryKeySelective(DeviceType type) {
        return deviceTypeMapper.updateByPrimaryKeySelective(type);
    }
}
